﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace DoAn
{
    public partial class frmSuaHang : Form
    {
        public frmSuaHang()
        {
            InitializeComponent();
        }
        public frmSuaHang(string MaHang):this()
        {
            HangHoaDTO hhDTO = new HangHoaDTO();
            HangHoaBUS hhBUS = new HangHoaBUS();
            hhDTO = hhBUS.LayHangTheoMa(MaHang);
            LoaiHangBUS lhBus = new LoaiHangBUS();             
            lbMaHang.Text = hhDTO.MaHang;
            txtTenHang.Text = hhDTO.TenHang;

            List<LoaiHangDTO> listlh = new List<LoaiHangDTO>();
            LoaiHangBUS lh = new LoaiHangBUS();
            listlh = lh.LayLoaiHang();
            cbbLoaiHang.DataSource = listlh;
            cbbLoaiHang.DisplayMember = "TenLoaiHang";
            cbbLoaiHang.ValueMember = "MaLoaiHang";

            List<NhaCCDTO> ListNhaCC = new List<NhaCCDTO>();
            NhaCCBUS ncc = new NhaCCBUS();
            ListNhaCC = ncc.LayNhaCC();
            cbbNhaCC.DataSource = ListNhaCC;
            cbbNhaCC.DisplayMember = "TenNCC";
            cbbNhaCC.ValueMember = "MaNCC";

            NhaCCBUS nccbus = new NhaCCBUS();            
            cbbNhaCC.Text = nccbus.LayNCCTheoMa(hhDTO.MaNCC).TenNCC;
            LoaiHangBUS lhbus = new LoaiHangBUS();
            cbbLoaiHang.Text = lhbus.LayLoaiHangTheoMa(hhDTO.MaLoaiHang).TenLoaiHang;

            txtSoLuong.Text = hhDTO.SoLuong.ToString();
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool SuaHangHoa()
        {                                   
            if (txtTenHang.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập Tên Hàng.", "Lỗi");
                return false;
            }
            List<LoaiHangDTO> listlhdto = new List<LoaiHangDTO>();
            LoaiHangBUS lhbus = new LoaiHangBUS();
            listlhdto = lhbus.LayLoaiHang();
            int malh = -1;
            for (int i = 0; i < listlhdto.Count; i++)
                if (listlhdto[i].TenLoaiHang == cbbLoaiHang.Text)
                {
                    malh = listlhdto[i].MaLoaiHang;
                    break;
                }
            if (malh == -1)
            {
                MessageBox.Show("Bạn cần chọn một loại hàng.", "Lỗi");
                return false;
            }
            NhaCCBUS nccbus = new NhaCCBUS();
            List<NhaCCDTO> listnccdto = new List<NhaCCDTO>();
            listnccdto = nccbus.LayNhaCC();
            int mancc = -1;
            for (int i = 0; i < listnccdto.Count; i++)
                if (listnccdto[i].TenNCC == cbbNhaCC.Text)
                {
                    mancc = listlhdto[i].MaLoaiHang;
                    break;
                }
            if (mancc == -1)
            {
                MessageBox.Show("Bạn cần chọn một nhà cung cấp.", "Lỗi");
                return false;
            }            

            HangHoaBUS hh = new HangHoaBUS();
            HangHoaDTO hhDto = new HangHoaDTO();
            hhDto.MaHang = lbMaHang.Text;
            hhDto.TenHang = txtTenHang.Text;
            hhDto.MaLoaiHang = malh;
            hhDto.MaNCC = mancc;
            hhDto.SoLuong = int.Parse(txtSoLuong.Text);
            hh.SuaHangHoa(hhDto);            
            return true;    
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {            
            if(SuaHangHoa())
                this.Close();
        }

        private void txtTenHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (SuaHangHoa())
                    this.Close();
            }
        }

        private void cbbLoaiHang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (SuaHangHoa())
                    this.Close();
            }
        }

        private void cbbNhaCC_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (SuaHangHoa())
                    this.Close();
            }
        }

        private void txtSoLuong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (SuaHangHoa())
                    this.Close();
            }
        }
    }
}
